package com.escape.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * @Author: escape_zebra
 * @CreateTime: 2025-01-02  16:13
 * @Description: TODO
 * @Version: 1.0
 */
public class DruidEncryptUtil {

    // 公钥
    private static String publicKey;
    // 私钥
    private static String privateKey;

    static {
        try {
            // 1. 获取密钥对
            String[] keyPair = ConfigTools.genKeyPair(512);
            // 2. 给公私钥赋值
            publicKey = keyPair[1];
            privateKey = keyPair[0];
            System.out.println("公钥:" + publicKey);
            System.out.println("私钥:" + privateKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }
    }

    public static String encrypt(String plainText) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey, plainText);
        System.out.println("encrypt:" + encrypt);
        return encrypt;
    }

    public static String decrypt(String encryptText) throws Exception {
        String decrypt = ConfigTools.decrypt(publicKey, encryptText);
        System.out.println("decrypt:" + decrypt);
        return decrypt;
    }

    public static void main(String[] args) throws Exception {
        String encrypt = DruidEncryptUtil.encrypt("escapeicon");
        System.out.println(encrypt);
    }
}
